﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

public class FunctionSignature : ModifiedSignatureWithParameters
{
    internal FunctionSignature(SyntaxToken declareKeyword, Identifier name, SyntaxNode? parent) :
        base(SyntaxKinds.FunctionSignature, declareKeyword, name, parent)
    {
    }

    /// <summary>
    ///     如果函数带有返回值，返回true。
    /// </summary>
    public bool HasReturn => DeclareKeywordToken.IsNode(SyntaxKinds.FunctionKeywordToken);


    protected override bool VerifyModifierToken(SyntaxToken token)
    {
        if (!token.IsNode(SyntaxKinds.NullableKeywordToken))
        {
            return false;
        }

        if (IsNullable)
        {
            return false;
        }

        IsNullable = true;
        return true;
    }
}


public static partial class NodeFactory
{
    public static FunctionSignature CreateFunctionSignature(SyntaxNode? parent, SyntaxToken declareKeywordToken,
        Identifier identifierName, IEnumerable<ParameterSignature>? parameters, TypeBinding? binding)
    {
        Debug.Assert(declareKeywordToken.IsOneOfNode(SyntaxKinds.FunctionKeywordToken,
            SyntaxKinds.SubKeywordToken));
        FunctionSignature signature = new(declareKeywordToken, identifierName, parent);

        if (parameters != null)
        {
            foreach (ParameterSignature param in parameters)
            {
                signature.AddParameter(param);
            }
        }

        if (binding != null)
        {
            signature.Update(binding);
        }

        return signature;
    }
}